package com.cto.uaa.swagger;

import com.cto.uaa.route.NacosRouteDefinitionRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.support.NameUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.SwaggerResource;
import springfox.documentation.swagger.web.SwaggerResourcesProvider;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 跨域配置
 */
@Component
@EnableConfigurationProperties(SwaggerAggProperties.class)
@Primary
public class SwaggerProvider implements SwaggerResourcesProvider {
    private final RouteLocator routeLocator;
    private final GatewayProperties gatewayProperties;

    @Autowired
    NacosRouteDefinitionRepository nacosRouteDefinitionRepository;
    @Resource
    private SwaggerAggProperties swaggerAggProperties;

    public SwaggerProvider(RouteLocator routeLocator, GatewayProperties gatewayProperties) {
        this.routeLocator = routeLocator;
        this.gatewayProperties = gatewayProperties;
    }

    @Override
    public List<SwaggerResource> get() {
        List<SwaggerResource> resources = new ArrayList<>();
        //结合application.yml中的路由配置，只获取有效的route节点
        nacosRouteDefinitionRepository.getRouteDefinitions().subscribe(
                routeDefinition -> routeDefinition.getPredicates().stream()
                .filter(predicateDefinition ->swaggerAggProperties.isShow(routeDefinition.getId()) && ("Path").equalsIgnoreCase(predicateDefinition.getName()))
                .forEach(predicateDefinition ->  resources.add(
                    swaggerResource(
                            routeDefinition.getId(),
                            predicateDefinition.getArgs().get("pattern").replace("/**", swaggerAggProperties.getApiDocsPath())
                        )
                    )
                )
        );
        return resources;
    }

    /**
    * @Description: 非动态路由可从application.yml文件中直接读取·
    */
//    @Override
//    public List<SwaggerResource> get() {
//        List<SwaggerResource> resources = new ArrayList<>();
//        Set<String> routes = new HashSet<>();
//        //取出Spring Cloud Gateway中的route
//        routeLocator.getRoutes().subscribe(route -> routes.add(route.getId()));
//        //结合application.yml中的路由配置，只获取有效的route节点
//        gatewayProperties.getRoutes().stream().filter(
//                routeDefinition -> (
//                        routes.contains(routeDefinition.getId()) && swaggerAggProperties.isShow(routeDefinition.getId())
//                )
//        ).forEach(routeDefinition -> routeDefinition.getPredicates().stream()
//                .filter(predicateDefinition -> ("Path").equalsIgnoreCase(predicateDefinition.getName()))
//                .forEach(predicateDefinition -> resources.add(
//                    swaggerResource(
//                            routeDefinition.getId(),
//                            predicateDefinition.getArgs().get(NameUtils.GENERATED_NAME_PREFIX + "0").replace("/**", swaggerAggProperties.getApiDocsPath())
//                        )
//                    )
//                )
//        );
//        return resources;
//    }

    private SwaggerResource swaggerResource(String name, String location) {
        SwaggerResource swaggerResource = new SwaggerResource();
        swaggerResource.setName(name);
        swaggerResource.setLocation(location);
        swaggerResource.setSwaggerVersion(swaggerAggProperties.getSwaggerVersion());
        return swaggerResource;
    }
}